# -*- coding: utf-8 -*-
"""
Created on Wed Apr 22 11:57:04 2020

@author: Administrator
"""

import os
import pickle
import time

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
import json

brower = webdriver.Chrome('C:/Users/Administrator/AppData/Local/Google/Chrome/Application/chromedriver.exe')
wait = WebDriverWait(brower, 10)



def getTaobaoCookies():
    # get login taobao cookies
    url = "http://srm.gaojihealth.cn/Index/index"
    brower.get("http://srm.gaojihealth.cn/DataCenter/DataDown")
    while True:
        print("Please login in taobao.com!")
        time.sleep(3)
        # if login in successfully, url  jump to www.taobao.com
        while brower.current_url ==  url:
            tbCookies  = brower.get_cookies()
            #brower.quit()
            cookies = {}
            for item in tbCookies:
                cookies[item['name']] = item['value']
            outputPath = open('taobaoCookies.pickle','wb')
            pickle.dump(cookies,outputPath)
            outputPath.close()
            return cookies
def readTaobaoCookies():
    # if hava cookies file ,use it 
    # if not , getTaobaoCookies()
    if os.path.exists('taobaoCookies.pickle'):
        readPath = open('taobaoCookies.pickle','rb')
        tbCookies = pickle.load(readPath)
    else:
        tbCookies = getTaobaoCookies()
    return tbCookies


ck = getTaobaoCookies()
tbCookies = readTaobaoCookies()

brower.get("http://srm.gaojihealth.cn/Index/index")
for cookie in tbCookies:
    brower.add_cookie({
        "domain":".taobao.com",
        "name":cookie,
        "value":tbCookies[cookie],
        "path":'/',
        "expires":None
    })
    
brower.get("http://srm.nepstar.cn/ELSServer_HWXC/default2.jsp?account=110829_1001&loginChage=N&telphone1=18279409642")
brower.get("http://srm.nepstar.cn")

brower = webdriver.Chrome('C:/Users/Administrator/AppData/Local/Google/Chrome/Application/chromedriver.exe')
wait = WebDriverWait(brower, 10)

dictCookies = brower.get_cookies()
jsonCookies = json.dumps(dictCookies)
# print(jsonCookies)
with open('anquan.txt', 'w') as f:
   f.write(jsonCookies)
 
    
obj = webdriver.Chrome('C:/Users/Administrator/AppData/Local/Google/Chrome/Application/chromedriver.exe')
wait = WebDriverWait(obj, 10)   
with open('anquan.txt', 'r', encoding='utf8') as f:
      listCookies = json.loads(f.read())
  #print('%%%%%%%%%%%%%%%%%', listCookies)
obj.get('http://srm.nepstar.cn')
obj.delete_all_cookies()
for cookie in listCookies:
    obj.add_cookie(cookie)
obj.get('http://srm.nepstar.cn/ELSServer_HWXC/default2.jsp?account=110829_1001&loginChage=N&telphone1=18279409642')
  # 读取完cookie刷新页面
obj.refresh()
obj.get_cookies()



browser = webdriver.Chrome('C:/Users/Administrator/AppData/Local/Google/Chrome/Application/chromedriver.exe')
browser.maximize_window()   #设置浏览器大小：全屏
browser.get('http://60.217.250.254:8765/')  
def input_a(id_1,value):
    input_box = obj.find_element_by_id(id_1)
    try:
        #输入内容：selenium
        input_box.send_keys(value)
        print('输入成功')
    except Exception as e:
        print('fail')
def input_b(id_1,value):
    input_box = obj.find_element_by_xpath(id_1)
    try:
        #输入内容：selenium
        input_box.send_keys(value)
        print('输入成功')
    except Exception as e:
        print('fail')
def button_a(xpath):
    button = obj.find_element_by_xpath(xpath)
    try:
        #点击搜索按钮
        button.click()
        print('成功搜索')
    except Exception as e:
        print('fail搜索')  
def clear_a(id_1):
    input_box = obj.find_element_by_xpath(id_1)
    try:
        input_box.clear()
        print('成功清空输入框')
    except Exception as e:
        print('fail清空输入框')
    #输出内容：成功清空输入框

    
input_a('username','中山市中智')
input_a('password','zsszz')
button = browser.find_element_by_id('notlogin')
try:
    #点击搜索按钮
    button.click()
    print('成功搜索')
except Exception as e:
    print('fail搜索')
#输出内容：成功搜索
    
    
button_a('//*[@id="treeMenu"]/li[7]/a')
button_a('//*[@id="salesOutSourcingInfoManage"]')

button_a('//*[@id="tab-nav-item-editAccount"]/a')
button_a('//*[@id="tab-nav-item-editAccount"]/a/i[2]')
button_a('//*[@id="export"]/a')
button_a('/html/body/div[1]/div[1]/nav/div/div/ul/li[2]/a')
button_a('//*[@id="tab-nav-item-editAccount"]/a')
button_a('//*[@id="tab-nav-item-editAccount"]/a')
button_a('//*[@id="tab-nav-item-editAccount"]/a')
button_a('//*[@id="tab-nav-item-editAccount"]/a')

button_js = "document.getElementById('export').click();"

obj.execute_script(button_js)

# 先通过xpath定位到iframe
xf = obj.find_element_by_xpath('/html/body/div[2]/div/nav[2]/div[3]/iframe')
# 再将定位对象传给switch_to.frame()方法
obj.switch_to.frame(xf)
button_a('/html/body/div[1]/div[2]/form/div[1]/div/div/span')
obj.switch_to.parent_frame()  
xf = obj.find_element_by_xpath('/html/body/div[4]/div/table/tbody/tr[2]/td/div/iframe')
obj.switch_to.frame(xf)
button_a('/html/body/div/main/div[1]/div[1]/div[1]/table/thead/tr/th[2]/div/span/input')
button_a('/html/body/div/main/div[2]/button[1]')
obj.switch_to.parent_frame() 
xf = obj.find_element_by_xpath('/html/body/div[2]/div/nav[2]/div[3]/iframe')
# 再将定位对象传给switch_to.frame()方法
obj.switch_to.frame(xf)
input_b('/html/body/div[1]/div[2]/form/div[2]/div/div/input','2020-4-1')
input_b('/html/body/div[1]/div[2]/form/div[3]/div/div/input','2020-4-25')
    
button_a('/html/body/div[1]/div[2]/form/div[4]/div/div/div/p')

button_a('/html/body/div[1]/div[2]/form/div[5]/div/div/div/p')
button_a('/html/body/div[1]/div[2]/form/div[5]/div/div/div/div/ul/li[17]')
button_a('/html/body/div[1]/div[2]/form/button[2]')
button_a('/html/body/div[1]/div[3]/div[1]/div[1]/table/thead/tr/th[2]/div/span')
button_a('/html/body/div[1]/div[1]/nav/div/div/ul/li[1]/a')

obj.switch_to.parent_frame()  
xf = obj.find_element_by_xpath('/html/body/div[2]/div/nav[2]/div[4]/iframe')
obj.switch_to.frame(xf)

profile = webdriver.ChromeProfile()
profile.set_preference('browser.download.dir', 'd:\\shiyan\\01.xls')
profile.set_preference('browser.download.folderList', 2)
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/zip')

button_a('/html/body/div/div[1]/nav/div/div/ul/li[2]/a')
button_a('/html/body/div[1]/div[1]/nav/div/div/ul/li[1]/a')

obj.switch_to.parent_frame()  






obj.switch_to.frame("477fedea7d4366445ae191510cbe2139")




button = browser.find_element_by_xpath('//*[@id="menu"]/li[3]/a')
    
button_a('/html/body/div[1]/div[2]/form/div[2]/div/div/input')   
input_a('fbk10','2020-04-01')  
element1=browser.find_element_by_xpath('//*[@id="jerichotab_5"]/div[1]/div[1]')
element11=element1.find_element_by_xpath('/html/body/ul/li/a')
element11=element1.find_element_by_xpath('//*[@id="beginTima"]')
element11.clear()
element11.send_keys("2020-04-01")
    
obj.window_handles
obj.switch_to_window(3)   
frame1 = obj.find_element_by_css_selector("#tab-nav-item-salesOutSourcingInfoManage")
obj.switch_to_frame(frame1)
userName = obj.find_element_by_id("userName").text
print(userName)


    
try:
    input_box.clear()
    print('成功清空输入框')
except Exception as e:
    print('fail清空输入框')
#输出内容：成功清空输入框
    
#定位输入框
input_box = browser.find_element_by_id('kw')
#输入关键词：selenium
input_box.send_keys('selenium')
#模拟回车操作
try:
    input_box.submit()
    print('成功回车')
except Exception as e:
    print('fail')
#输出内容：成功回车